﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Olympic_email_to_future.Module;
using Olympic_email_to_future.BLLFactory;
using Olympic_email_to_future.IBLL;
using Olympic_email_to_future.DALFactory;
using Olympic_email_to_future.IDAL;
using Olympic_email_to_future.DAL;

namespace FutureMeWeb
{
    public partial class mymail : System.Web.UI.Page
    {
        public IMailBLL bll = BusinessFactory.CreateEmailBLL();
        PagedDataSource pds = new PagedDataSource();
        IList<MailInfo> listmail = new List<MailInfo>(); 
        public int currentPage = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["User"] != null)
            {
                UserInfo user = (UserInfo)Session["User"];
                AccountBtn.Text = user.userMailAddress;
                LoginBtn.Visible = false;
                RegisterBtn.Visible = false;
            }
            else
            {
                AccountBtn.Visible = false;
                LogoffBtn.Visible = false;
            }

            if(!IsPostBack)
            {
                setBind();
            }
        }


        

        protected void rptMailRead(object source, RepeaterCommandEventArgs e)
        {
            
        }

        public void setBind()
        {
            UserInfo user = (UserInfo)Session["User"];
            listmail = bll.dogetMailInfoByMailAddress(user.userMailAddress);
            int curpage=Convert.ToInt32(lbl_current.Text);//当前页数 在页面初始化为1

            pds.DataSource = listmail;
            pds.AllowPaging = true;
            pds.PageSize = 5;//设置每页大小
            pds.CurrentPageIndex = curpage - 1;//设置当前页索引  从0开始！！！！！
            lbl_pages.Text = pds.PageCount.ToString();
            lbl_counts.Text = pds.DataSourceCount.ToString();

            if (!IsPostBack)
            {
                //Response.Write("不是postback");
                for (int i = 1; i < pds.PageCount + 1; i++) 
                {
                    DropDownList1.Items.Add(new ListItem(i.ToString()));
                    link_up.Enabled = true;
                    link_down.Enabled = true;
                }
               
            }

            try
            {
                //Response.Write("   公共代码区");
                DropDownList1.SelectedValue = lbl_current.Text.ToString();
                Repeater1.DataSource = pds;
                Repeater1.DataBind();
            }
            catch (Exception e) { throw e; }

            
        }




        protected void LinkFirst_Click(Object sender, EventArgs e)
        {
            if (lbl_current.Text.ToString() != "1")
            {
                lbl_current.Text = "1";
                setBind();
            }
            else
            {
                
                //this.Page.ClientScript.RegisterStartupScript(this.GetType(),"","alert('已经是第一页')",true);
                lbl_current.Text = "1";
                setBind();
                //setBind();
            }
            
        }

        protected void LinkLast_Click(Object sender, EventArgs e)
        {
            if (lbl_current.Text.ToString() != lbl_pages.Text.ToString())
            {
                lbl_current.Text = lbl_pages.Text.ToString();
                setBind();
            }
            else
            {
                lbl_current.Text = lbl_pages.Text.ToString();
                setBind();
                //Response.Write("23");
                
                //this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('已经是最后一页')", false);
                
            }
        }

        protected void LinkUp_Click(Object sender, EventArgs e)
        {
            if (!lbl_current.Text.ToString().Equals("1"))
            {
                lbl_current.Text = (Convert.ToInt32(lbl_current.Text.ToString()) - 1).ToString();
                setBind();
            }
            
        }

        protected void LinkDown_Click(Object sender, EventArgs e)
        {
            if (lbl_current.Text.ToString() != lbl_pages.Text.ToString())
            {
                lbl_current.Text = (Convert.ToInt32(lbl_current.Text.ToString()) + 1).ToString();    
                setBind();
            }
        }

        protected void dropdownlist1_DropDownListChanged(Object sender, EventArgs e)
        {

            //Response.Write((this.DropDownList1.SelectedValue).ToString());
            
            lbl_current.Text = DropDownList1.SelectedValue;
            setBind();
        }


    }
}
